﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace EmailMarketingCampaign
{
    public partial class CreateCamp : System.Web.UI.Page
    {
        Campaign c = new Campaign();
        Message m = new Message();
        CampaignControl cc = new CampaignControl();

        protected void Page_Load(object sender, EventArgs e)
        {
            drpThem.DataBind();
            if (!Page.IsPostBack)
            {
                if (((Campaign)Session["campaign"]) != null)
                {
                    txtCampName.Text = ((Campaign)Session["campaign"]).getCampaignName().ToString();
                    txtCampDes.Text = ((Campaign)Session["campaign"]).getDescription().ToString();
                    drpThem.SelectedValue = ((Campaign)Session["campaign"]).getThemeID().ToString();
                }
                else
                {
                    txtCampName.Text = "";
                    txtCampDes.Text = "";
                }
            }


        }

        protected void btnAddThem_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddTheme.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool check = false;
            if ((Campaign)Session["campaign"] != null)
            {
                ((Campaign)Session["campaign"]).setDescription(txtCampDes.Text.ToString());
                ((Campaign)Session["campaign"]).setThemeID(int.Parse(drpThem.SelectedValue));
                ((Campaign)Session["campaign"]).setCampaignName(txtCampName.Text.ToString());
                check = cc.checkExitCampRecord(((Campaign)Session["campaign"]));
                if (check == true)
                {

                    this.Response.Write(" <script>alert('exiciting record '); </script>");
                }
                else
                {
                    cc.saveEmail(((Campaign)Session["campaign"]));
                    this.Response.Write(" <script>alert('save sucseeful.'); </script>");
                }



            }
            else
            {


                Session["campaign"] = c;
                int themeID = int.Parse(drpThem.SelectedValue);
                string campaignName = txtCampName.Text;
                string camDescription = txtCampDes.Text;


                c.setDescription(txtCampDes.Text.ToString());
                c.setThemeID(int.Parse(drpThem.SelectedValue));
                c.setCampaignName(txtCampName.Text.ToString());
                c.setFrom("");
                c.setTo("");
                c.setSchedule("");

                c.setCOMUniquelID(Session["COMUniquelID"].ToString());

                check = cc.checkExitCampRecord(c);
                if (check == true)
                {

                    this.Response.Write(" <script>alert('exiciting record '); </script>");
                }
                else
                {
                    cc.insertNewEmail(themeID, campaignName, camDescription, Session["COMUniquelID"].ToString());
                    this.Response.Write(" <script>alert('save sucseeful.'); </script>");
                }


            }

        }



        protected void btnNext_Click(object sender, EventArgs e)
        {
            bool check = false;

            if ((Campaign)Session["campaign"] != null)
            {
                ((Campaign)Session["campaign"]).setDescription(txtCampDes.Text.ToString());
                ((Campaign)Session["campaign"]).setThemeID(int.Parse(drpThem.SelectedValue));
                ((Campaign)Session["campaign"]).setCampaignName(txtCampName.Text.ToString());
                check = cc.checkExitCampRecord(((Campaign)Session["campaign"]));
                if (check == true)
                {

                    this.Response.Write(" <script>alert('exiciting record '); </script>");

                }
                else
                {
                    cc.saveEmail(((Campaign)Session["campaign"]));
                }

            }
            else
            {

                Session["campaign"] = c;
                int themeID = int.Parse(drpThem.SelectedValue);
                string campaignName = txtCampName.Text;
                string camDescription = txtCampDes.Text;


                c.setDescription(txtCampDes.Text.ToString());
                c.setThemeID(int.Parse(drpThem.SelectedValue));
                c.setCampaignName(txtCampName.Text.ToString());
                c.setFrom("");
                c.setTo("");
                c.setSchedule("");

                c.setCOMUniquelID(Session["COMUniquelID"].ToString());

                check = cc.checkExitCampRecord(c);
                if (check == true)
                {

                    this.Response.Write(" <script>alert('exiciting record '); </script>");
                }
                else
                {
                    cc.insertNewEmail(themeID, campaignName, camDescription, Session["COMUniquelID"].ToString());
                }


            }

            Response.Redirect("TypeofMessage.aspx");
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("CampaignsList.aspx");
        }


    }
}
